1 module targets.wasm;
2 import commons;
3 import global_opts;
4 import serve;
5 
6 ChoiceResult prepareWASM(Choice* c, ref Terminal t, ref RealTimeConsoleInput input, in CompilationOptions cOpts)
7 {
8 	if(!hasLdc)
9 	{
10 		t.writelnError("WASM build requires ldc2 in path. Please install it before building to it.");
11 		return ChoiceResult.Error;
12 	}
13 	cached(() => timed(() => loadSubmodules(t, input)));
14 	if(!serverStarted)
15 	{
16 		t.writelnHighlighted("Attempt to start WebAssembly development server.");
17 		startServer();
18 		t.writelnSuccess("Development started at localhost:9000");
19 	}
20 	runEngineDScript(t, "releasegame.d", configs["gamePath"].str);
21 	putResourcesIn(t, getHipPath("build", "wasm", "build", "assets"));
22 
23 
24 	runEngineDScript(t, "gendir.d", 
25 		getHipPath("build", "release_game", "assets"),
26 		getHipPath("build", "wasm", "generated")
27 	);
28 	cached(() => timed(() => outputTemplateForTarget(t)));
29 	//The template may not be present
30 	outputTemplate(t, configs["gamePath"].str);
31 
32 	environment["DFLAGS"] = 
33 		"-I="~getHipPath("modules", "d_std", "source") ~" "~
34 		"-I="~getHipPath("dependencies", "runtime", "druntime", "arsd-webassembly") ~" " ~
35 		"-L-allow-undefined -d-version=CarelessAlocation";
36 
37 	with(WorkingDir(getHipPath))
38 	{
39 		if(timed(() =>waitDubTarget(t, "wasm", DubArguments()
40 			.command("build").compiler("ldc2").build("debug")
41 			.arch("wasm32-unknown-unknown-wasm").opts(cOpts))) != 0)
42 		{
43 			t.writelnError("Could not build for WebAssembly.");
44 			return ChoiceResult.Error;
45 		}
46 		environment["DFLAGS"]= "";
47 		timed(() => waitDub(t, DubArguments().command("run wasm-sourcemaps").runArgs("hipreme_engine.wasm --include-sources=true")));
48 
49 		foreach(file; ["hipreme_engine.wasm", "hipreme_engine.wasm.map"])
50 			std.file.rename(file, buildPath("build", "wasm", "build", file));
51 		t.writelnSuccess("Succesfully built for WebAssembly. Listening on http://localhost:9000");
52 		pushWebsocketMessage("reload");
53 		cached(() => cast(void)openDefaultBrowser("http://localhost:9000"));
54 	}
55 
56 	return ChoiceResult.None;
57 }